from django.db import models
from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.OneToOneField(User,on_delete = models.CASCADE)
    nickname = models.CharField(max_length = 20)

    def __str__(self):
        return '<Profile: %s for %s>'% (self.nickname, self.user.username)

#python的动态绑定
def get_nickname_or_username(self):
    if Profile.objects.filter(user=self).exists():
        profile = Profile.objects.get(user=self)
        return profile.nickname
    else:
        return self.username
User.get_nickname_or_username = get_nickname_or_username


